# This code is hosted on http://code.google.com/p/lenthorp/
# Freely available for use in applications, but should NOT be modified
# Email all comments to lenthorpresearch@gmail.com

import PricingEngine_HestonHullWhiteAnalyticalMod
import imp
imp.reload(PricingEngine_HestonHullWhiteAnalyticalMod)
import ModelParametersMod
imp.reload(ModelParametersMod)
from PricingEngine_HestonHullWhiteAnalyticalMod import *
from CalibratorMod import *
import time

def runTest():
    x=time.time()
    mp = ModelParameters()
    mp.p['type'] = 'call'
    mp.p['rate'] = 0.0
    mp.p['initial'] = 100
    mp.p['isPartial'] = True
    mp.p['isFFT'] = False
    mp.p['HullWhite1Params_Lambda'] = 0.01
    mp.p['HullWhite2Params_Lambda'] = 0.05
    mp.p['HullWhite1Params_Nu'] = 0.007
    mp.p['HullWhite2Params_Nu'] = 0.012
    mp.p['Corr_HW1_HW2'] = 0.0
    mp.p['HestonParams_V0'] = 0.01
    mp.p['Corr_HW1_Hest'] = 0
    mp.p['Corr_HW2_Hest'] = 0
    mp.p['Corr_HW1_HestVol'] = 0
    mp.p['Corr_HW2_HestVol'] = 0
    hhwe = HestonHullWhiteAnalytical(mp)

    op = OptimiseParameters()
    op.p['HestonParams_Gamma'] = 0.2
    op.p['HestonParams_Kappa'] = 0.5
    op.p['HestonParams_Mean'] = 0.01
    op.p['Corr_Hest_HestVol'] = -0.8
    op.setBound('HestonParams_Gamma', 0, True)
    op.setBound('HestonParams_Gamma', 100, False)
    op.setBound('HestonParams_Kappa', 0, True)
    op.setBound('HestonParams_Kappa', 100, False)
    op.setBound('HestonParams_Mean', 0, True)
    op.setBound('HestonParams_Mean', 0.5, False)
    op.setBound('Corr_Hest_HestVol', -1, True)
    op.setBound('Corr_Hest_HestVol', 1, False)

    ##targetPrice = 86.0
    pp = PricingParameters(['strike', 'tenor'])
    pp.addPricingSet([30, 1], 58.38)
    pp.addPricingSet([70, 5], 24.0)
    pp.addPricingSet([100, 2], 7.12)
    pp.addPricingSet([130, 10], 7.34)
    pp.addPricingSet([180, 25], 6.25)
    pp.addPricingSet([240, 30], 4.45)

    dm = SumOfSquaresDistance()
    hhwe.setUpParameters(pp,op)
    ##print(hhwe.getPrice())
    c = ScipyFminSLSQPCalibrator(hhwe, pp, op, dm)
    res = hhwe.getPrice()
    ##print(c.calibrate())
    ##print('')
    ##print(time.time()-x)
    return res

if __name__ == "__main__":
    res = runTest()
    print(res)